<script setup>
import { nextTick, reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { Message } from "@arco-design/web-vue";
import projectApi from "@/api/projectApi";
import constants from "@/utils/constants";

const submitForm = ref();
const searchForm = ref();
const searchModel = reactive({
  gradeId: "",
});
const visible = ref(false);
let submitData = ref({});
const loading = ref(false);
const pagination = reactive({
  total: 0,
  current: 1,
  pageSize: 15,
  showPageSize: true,
  pageSizeOptions: [15, 25, 30, 50],
  showTotal: true,
  pageChange: (current) => {
    pagination.current = current;
    search(current, pagination.pageSize);
  },
  pageSizeChange(size) {
    pagination.pageSize = size;
    search(pagination.current, size);
  },
});

const columns = [
  {
    title: "年级",
    dataIndex: "gradeName",
  },
  {
    title: "性别",
    slotName: "sex",
  },
  {
    title: "项目名称",
    dataIndex: "projectName",
  },
  {
    title: "成绩",
    dataIndex: "testValue",
  },
  {
    title: "分数",
    dataIndex: "score",
  },
  {
    title: "等级",
    dataIndex: "level",
  },
];
const data = ref([]);

const editRecord = (record) => {
  visible.value = true;
  nextTick(() => {
    submitForm.value.resetFields();
    submitData.value = Object.assign({}, record);
  });
};

const deleteRecord = (record) => {
  projectApi
    .delete({
      id: record.id,
    })
    .then((res) => {
      search(1, 15);
    });
};

const search = (current = 1, size = 15) => {
  var params = {
    pageNo: current,
    pageSize: size,
    queryParam: Object.assign({}, searchModel),
  };
  loading.value = true;
  projectApi.standardPage(params).then((res) => {
    data.value = res.records;
    pagination.total = res.total;
    loading.value = false;
  });
};

const refresh = () => {
  search(1, 15);
};

const reset = () => {
  searchForm.value.resetFields();
};

search();
</script>

<template>
  <div class="main-con">
    <a-card>
      <a-row>
        <a-col :flex="1">
          <a-form ref="searchForm" :model="searchModel" :label-col-props="{ span: 5 }" :wrapper-col-props="{ span: 19 }"
            label-align="left">
            <a-row :gutter="24">
              <a-col :span="8">
                <a-form-item field="prjCode" label="体测项目">
                  <a-select v-model="searchModel.prjCode" placeholder="请选择项目">
                    <a-option v-for="item in constants.projectList" :key="item.value" :value="item.value"
                      :label="item.label"></a-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-form-item field="sex" label="性别">
                  <a-select v-model="searchModel.sex" placeholder="请选择性别">
                    <a-option value="1">男</a-option>
                    <a-option value="2">女</a-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-form-item field="gradeId" label="年级">
                  <a-select v-model="searchModel.gradeId" placeholder="请选择年级">
                    <a-option v-for="item in constants.gradeList" :key="item.value" :value="item.value"
                      :label="item.label"></a-option>
                  </a-select>
                </a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </a-col>
        <a-divider style="height: 84px" direction="vertical" />
        <a-col :flex="'86px'" style="text-align: right">
          <a-space direction="vertical" :size="18">
            <a-button type="primary" @click="refresh">
              <template #icon>
                <icon-search />
              </template>
              查询
            </a-button>
            <a-button @click="reset">
              <template #icon>
                <icon-refresh />
              </template>
              重置
            </a-button>
          </a-space>
        </a-col>
      </a-row>
    </a-card>
    <a-card style="margin-top: 20px">
      <a-table :columns="columns" row-key="id" :loading="loading" :data="data" @page-change="pagination.pageChange"
        @page-size-change="pagination.pageSizeChange" :pagination="pagination" :bordered="false">
        <template #sex="{ record }">
          {{ record.sex == "1" ? "男" : " 女" }}
        </template>
        <template #action="{ record }">
          <a-space>
            <a-button type="text" @click="editRecord(record)">编辑</a-button>
            <a-popconfirm content="确认要删除该数据吗？" @ok="deleteRecord(record)">
              <a-button type="text">删除</a-button>
            </a-popconfirm>
          </a-space>
        </template>
      </a-table>
    </a-card>
  </div>
</template>

<style scoped></style>
